{% extends "base.html" %}
{% block head %}
{{ super() }}
{% endblock %}
{% block content %}
<div class="container">
  <h1>Chain: {{ chain.name }}</h1>
  <button class="btn btn-danger" onclick="window.location.href='/chains/{{ chain.id }}/{{chain.table.name}}/delete'">Delete</button>
  <button class="btn btn-warning" onclick="window.location.href='/chains/{{ chain.id }}/{{chain.table.name}}/flush'">Flush</button>
  <div class="row">
    <div class="col-md-6">
      <h2>Details:</h2>
      <p><strong>Table:</strong> {{ chain.table.name }}</p>
      <p><strong>Hook type:</strong> 
        {% if chain.hook_type == None %}
          This chain is not associated with a hook
        {% else %}
        {{ chain.hook_type }}</p>
        {% endif %}
      {% if chain.type != None %}
        <p><strong>Type:</strong> {{ chain.type }}</p>
      {% endif %}
      {% if chain.description == None %}
        <p><strong>Description:</strong> No description</p>
      {% else %}
        <p><strong>Description:</strong> {{ chain.description }}</p>
      {% endif %}
      {% if chain.policy != None %}
        <p><strong>Policy:</strong> {{ chain.policy }}</p>
      {% endif %}
      {% if chain.priority != None %}
        <p><strong>Priority:</strong> {{ chain.priority }}</p>
      {% endif %}
      <p><strong>Family:</strong> {{ chain.table.family }}</p>
    </div>
    <div class="col-md-6">
      <h2>Rules:</h2>
      <table class="table personal-table">
        <thead>
          <tr>
            <th>Handle</th>
            <th>Expresion</th>
            <th>Description</th>
            <th>Action</th>
          </tr>
        </thead>
        <tbody>
        {% for rule in chain.rules %}
        {% if rule.table().name == chain.table.name and rule.family == chain.family %}

          <tr>
            <td>{{ rule.handle }}</td>
            <td>{% for statement in rule.expr %}
              {% if statement.get("match", None) != None %}
              {{ statement.get("match", None)["left"] }} {{ statement.get("match", None)["op"]  }} {{ statement.get("match", None)["right"] }}
              <br></br>
              {% else %}
              {% set first_key = statement.keys()|list|first %}
              {{ first_key }}
              <br></br>
              {% endif %}
              {% endfor %}
            </td>
            <td>
              {% if rule.description == None %}
                No description
              {% else %}
              {{ rule.description }}
              {% endif %}</td>
            <td><button class="btn btn-primary" onclick="window.location.href='/rules/{{rule.id}}'">View</button></td>
          </tr>
        {% endif %}
        {% endfor %}
        </tbody>
    </div>
  </div>
</div>
{% endblock %}